package com.power.sifa.aop.service.impl;

import com.power.sifa.aop.service.SMService;
import com.power.sifa.util.SM4Util;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class SMServiceImpl implements SMService {

    //密钥
    @Value("${sm4.key}")
    private String key;
    //加密开关
//    @Value("${sm4.open}")
//    private boolean open;

    /**
     * 解密
     *
     * @param text
     * @return
     * @throws Exception
     */
    @Override
    public String decrypt(String text) throws Exception {
//        if (!open) {
//            return "请打开加密/解密开关！";
//        }
        return SM4Util.decryptEcb(key, text);
    }

    /**
     * 加密
     *
     * @param text
     * @return
     * @throws Exception
     */
    @Override
    public String encrypt(String text) throws Exception {
//        if (!open) {
//            return "请打开加密/解密开关！";
//        }
        return SM4Util.encryptEcb(key, text);
    }

    /**
     * 获取密钥
     * @return
     */
    @Override
    public String getKey() {
        return this.key;
    }

    @Override
    public Boolean verifyEcb(String cipherText, String paramStr) throws Exception {
        return SM4Util.verifyEcb(key, cipherText, paramStr);
    }
}
